<?php
	/**
	 * MySQL database adapter.
	 * @author IMP
	 */
	
	class MySQLDatabase extends Database {
		private $resource, $host;
		
		protected function __connect($host, $user, $pass){
			$this->resource = mysql_pconnect($host, $user, $pass);
			$this->host = $host;
			
			if(!$this->resource){
				return false;
			}else{
				return true;
			}
		}
		
		protected function __selectDatabase($database){
			return mysql_select_db($database, $this->resource);
		}
		
		protected function __query($string){
			$res = mysql_query($string, $this->resource);
			
			if(!$res){
				return new DatabaseError(mysql_errno($this->resource), mysql_error($this->resource), $string);
			}else{
				return new MySQLQueryResult($res);
			}
		}
		
		protected function __execute($string){
			$res = mysql_query($string, $this->resource);
			
			if(!$res){
				return new DatabaseError(mysql_errno($this->resource), mysql_error($this->resource), $string);
			}
		}
		
		protected function __getInfo(){
			$version = mysql_get_server_info($this->resource);
			$port = 3306;
			$host = $this->host;
			
			if(preg_match('#^(.*?):([0-9]+)$#', $host, $hits)){
				$port = $hits[2];
				$host = $hits[1];
			}
			
			return new DatabaseInformation($host, $port, get_class($this), 'MySQL-' . $version);
		}
		
		protected function __disconnect(){
			mysql_close($this->resource);
		}
		
		protected function __escape($str){
			return mysql_real_escape_string($str, $this->resource);
		}
	}
?>